package net.xuele.learn.leetcode;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * @Author: yubo
 * @date: 12:55 2020/4/26
 * @Description: 类描述
 */

public class MergeKLists {

    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) return null;
        PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, new Comparator<ListNode>() {
            @Override
            public int compare(ListNode o1, ListNode o2) {
                if (o1.val < o2.val) return -1;
                else if (o1.val == o2.val) return 0;
                else return 1;
            }
        });
        ListNode dummy = new ListNode(0);
        ListNode p = dummy;
        for (ListNode node : lists) {
            if (node != null) queue.add(node);
        }
        while (!queue.isEmpty()) {
            p.next = queue.poll();
            p = p.next;
            if (p.next != null) queue.add(p.next);
        }
        return dummy.next;

    }


    public boolean isPalindrome(ListNode head) {
        if (head == null) {
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        ListNode newNode = null;
        while (fast.next != null && fast.next.next != null) {
            fast = fast.next.next;
            fast = fast.next.next;
            ListNode next = slow.next;
            slow.next = newNode;
            newNode = slow;
            slow = next;
        }

        return true;
    }
}
